<?php

class plasamodel extends CI_Model {

    //plasa login checking
    /* public function is_userplasa_login_correct($userplasa = null, $password = null) {
      if ($userplasa != null && $password != null) {
      $this->db->where('userplasa', $userplasa);
      $this->db->where('password', $password);
      $query = $this->db->get('plasa');
      if ($query->num_rows() == 1) {
      return 1;
      } else {
      return 0;
      }
      } else {
      return 0;
      }
      } */

    // add plasa
    public function add_plasa($name = null, $address = null, $pic_contact = null) {
        $alert = $name . $address . $pic_contact;
        if ($name !== null && $address !== null && $pic_contact !== null) {
            $add_plasa = array(
                'name' => $name,
                'address' => $address,
                'pic_contact' => $pic_contact
            );
            return $this->add_plasa_arr($add_plasa) . $alert;
        }
    }

    // add plasa tuple
    public function add_plasa_arr($plasa_tuple = null) {
        if ($plasa_tuple != null) {
            $insert = $this->db->insert('plasa', $plasa_tuple);
            return $insert;
        }
    }

    // get plasa info based on id
    public function get_plasa_info($plasa_id = null) {
        if ($plasa_id != null) {
            $this->db->where('plasa_id', $plasa_id);
            $query = $this->db->get('plasa');
            return $query;
        }
    }

    public function get_plasa_id($name = null) {
        if ($userplasa != null) {
            $this->db->where('name', $userplasa);
            $query = $this->db->get('plasa');
            return $query;
        }
    }

    // get info of all plasa
    public function get_all_plasa() {
        $this->db->select('plasa_id, name, address, pic_contact');
        $query = $this->db->get('plasa');
        return $query;
    }
    
    public function get_all_plasa_simple(){
        $this->db->select('plasa_id, name');
        $query = $this->db->get('plasa');
        return $query;
    }

    public function update_plasa($plasa_id = null, $name = null, $address = null, $pic_contact = null) {

        if ($plasa_id !== null) {

            $stack = array();

            if ($name !== null) {
                $stack['name'] = $name;
            }

            if ($address !== null) {
                $stack['address'] = $address;
            }

            if ($pic_contact !== null) {
                $stack['pic_contact'] = $pic_contact;
            }

            if ($stack !== null) {
                //print_r($stack);
                return $this->update_plasa_arr($plasa_id, $stack);
            }
        }
    }

    //update plasa by an array of plasa information
    public function update_plasa_arr($plasa_id = null, $update_tuple = null) {
        if (($plasa_id !== null) && ($update_tuple !== null)) {
            $this->db->where('plasa_id', $plasa_id);
            return $this->db->update('plasa', $update_tuple);
        }
    }

    //delete a plasa
    public function delete_plasa($plasa_id = null) {
        if ($plasa_id !== null)
            $this->db->delete('plasa', array('plasa_id' => $plasa_id));
        return $this->db->affected_rows();
    }

}

?>
